/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package control
import dao.ClientDAO
import entity.Client
import java.sql.Connection;
/**
 *
 * @author Tato
 */
class ControlClient {
    
   
    ClientDAO dao = new ClientDAO()
   
    def addClient(Client client){
        
        if(validateName(client.name) == false){
            
           "Use entre 3 y 50 caracteres"
        }
        
        if(validateIdNum(client.idNum) != true){
             validateIdNum(client.idNum)   
        }
        
        if(validateContact(client.contact) == false){
            "Use entre 3 y 40 caracteres"
        }
        
        if(validateCity(client.city) == false){
            "Use entre 3 y 40 caracteres"
        }
           
            
          if(validateName(client.name) == true && validateAbbreviation(client.abbreviation) == true &&
            validateIdNum(client.idNum) == true && validateContact(client.contact) == true && 
            validateCity(client.city) == true){
                
                key = dao.create(client,DBConnection.establishConection())
                if(key > 0){
            
                    client.id= key
                    "Cliente Agregado"
                }
                else{
                    
                    "Cliente no asignado"
                }
            }
        
    }
    
    def deleteClient(Client client){
        
        
        delete = dao.remove(client,DBConnection.establishConection())
        
        if(delete == true){
            "Borrado exitoso"
        }
        else{
            "Borrado falló"
        }     
    }
    
    def updateClient(Client client){
        
        if(dao.update(client,DBConnection.establishConection()) > 0){
               "Información actualizada"
            }
            
            else {
                "Información no actualizada"
            }
        
    }
    
     def findClient(Integer id){
         
         if(dao.find(id,DBConnection.establishConection())!= null  ){
               "Información actualizada"
            }
            
            else {
                "Información no actualizada"
            }  
    }
    
    def validateName(String nameP){
        if (nameP.length() >=3 && nameP.length() <= 50)
            true
        
        false
    }
    
    def assingAbbreviation(String nameC){
        //3letras    
                def abre = []
                def j=1
                abre[0] = nameC[0]
                def space = 0

                 for(int i=1; i< nameC.length();i=i+1){

                   if(nameC[i] == " " && j<=2){
                            abre[j] = nameC[i+1]
                            j=j+1
                            space = space + 1     
                     }


                    }
                    
        def abbreviation
                if(space == 2){
                    abbreviation = abre[0] + abre[1] + abre[2]
                }

                if(space == 1){
                    abbreviation = abre[0] + nameC[1] + abre[1] 
                }
                if(space == 0){
                    abbreviation = nameC[0] + nameC[1] + nameC[2]
                }

                
                return abbreviation.toUpperCase()
        
        
    }
    
    def validateAbbreviation(String nameC ){
        abrev = assingAbbreviation(nameC)
         List consult = dao.select_by_property(abbreviation,abrev)
                 if(consult.isEmpty()){
                        Client.setAbbreviation(abrev) 
                        true
                 }
                 
                else{
                    abrev = generator( ('A'..'Z').join(), 3 )
                    validateAbbreviation(abrev)
                }
    }
    
    def generator = { String alphabet, int n ->
        new Random().with {
            (1..n).collect { alphabet[ nextInt( alphabet.length() ) ] }.join()
        }
}
    
    
    def validateIdNum(String num){
        
        def x = num
        if(num.length() > 4){
            if(x.toInteger().class.name == "java.lang.Integer"){
                     true               
                }
                
            else{
                
                "Formato de documento incorrecto"
            }
        }
        
        "Longitud incorrecta"
    } 
    
     def validateContact(String contactP){
        
        if(contactP.length() >= 3 && contactP.length() <= 40)
            true
        
        false
    } 
    
       def validateCity(String cityP){
        
        if(cityP.length() >= 3 && cityP.length() <= 40)
            true
             
        false
            
    } 
    
	
}

